package bit.springaopdemo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
//@Component
@Aspect

public class AspectDemo2 {

    @Pointcut("execution(* bit.springaopdemo.controller.*.*(..)))")
    private void pt(){};
//    @Before("execution(* bit.springaopdemo.controller.TestController.*(..)))")
//    public void doBefore(ProceedingJoinPoint pjp) throws Throwable {///只有around才能执行方法
//       log.info("doBefore");
//       pjp.proceed();
//
//    }

//    @Before("pt()")
//    public void doBefore()  {
//       log.info("doBefore");
//    }
//
////    @After("execution(* bit.springaopdemo.controller.*.*(..)))")
//    @After("pt()")
//    public void doAfter() throws Throwable {
//        log.info("doAfter");
//
//    }
//
//    @AfterReturning("execution(* bit.springaopdemo.controller.*.*(..)))")
//    public void doAfterReturning(){
//        log.info("doAfterReturning");
//    }
//
//    @AfterThrowing("execution(* bit.springaopdemo.controller.*.*(..)))")
//    public void doAfterThrowing(){
//        log.info("doAfterThrowing");
//    }

    @Around("execution(* bit.springaopdemo.controller.*.*(..)))")//对上述代码进行了封装
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {//抛出异常，proceed后的不执行
        log.info("around开始...");
        Object result = null;
        try {
            result = pjp.proceed();//执行的是throwable
        } catch (Throwable e) {
            log.error("抛出异常....");
            throw e;
        }

        log.info("around结束...");
        return result;
    }
}
